package com.longway.core.ext.excel.formats.model.emulate;

public class TokenAnalysis
{
    private String formula;
    private int position;
    private String[] splitStr = {"+", "-", "*", "/", "(", ")", " "};
    private String[] skipStr = {" "};

    public TokenAnalysis(String formula)
    {
        this.formula = formula;
        position = 0;
    }

    private void skipStr()
    {
        String str = null;
        boolean founded = false;
        do
        {
            founded = false;
            str = formula.substring(position, position + 1);
            for (String skip : skipStr)
            {
                if (str.equalsIgnoreCase(skip))
                {
                    position++;
                    founded = true;
                    break;
                }
            }
            if (!founded)
            {
                break;
            }
        }
        while (str != null);
    }

    public String getToken()
    {
        skipStr();
        if (position < formula.length())
        {
            return getTokenString();
        }

        return null;
    }

    public String getTokenString()
    {
        String str = null;
        boolean founded = false;
        int oldPos = position;
        do
        {
            founded = false;
            str = formula.substring(position, position + 1);
            for (String skip : splitStr)
            {
                if (str.equalsIgnoreCase(skip))
                {
                	position++;
                    founded = true;
                    break;
                }
            }
            if (founded)
            {
                return formula.substring(oldPos, position);
            }
        }
        while (str != null);

        return formula.substring(oldPos);
    }
    public static void main(String[] args) {
    	   TokenAnalysis parse = new TokenAnalysis(" (1+2)*3/4");
    	   System.out.println(parse.getToken());
	}
}

